from django import template
from django.conf import settings

register = template.Library()

def disqus():
    site_name = getattr(settings, 'SITE_NAME', None)
    disqus_developer = getattr(settings, 'DISQUS_DEVELOPER', False)
    if disqus_developer:
        disqus_developer_str = "var disqus_developer = 1;"
    else:
        disqus_developer_str = ""
    if site_name:
        return """<script type="text/javascript">
//<![CDATA[
%s
(function() {
    var links = document.getElementsByTagName('a');
    var query = '?';
    for(var i = 0; i < links.length; i++) {
    if(links[i].href.indexOf('#disqus_thread') >= 0) {
        query += 'url' + i + '=' + encodeURIComponent(links[i].href) + '&';
    }
    }
    document.write('<script charset="utf-8" type="text/javascript" src="http://disqus.com/forums/%s/get_num_replies.js' + query + '"></' + 'script>');
})();
//]]>
</script>""" % (disqus_developer_str, settings.SITE_NAME)
    else:
        return ""

register.simple_tag(disqus)






